package com.cswangdao.controller.admin;


import com.cswangdao.domain.model.MarketCategory;
import com.cswangdao.domain.vo.CategoryL1Vo;
import com.cswangdao.domain.vo.CategoryVo;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;

@RequestMapping("/admin/category")
@RestController
@Slf4j
public class AdminCategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * @description: 查询商品类目信息
     * @param: request, response
     * @return: void
     */
    @GetMapping("/list")
    public Result<PageResult<CategoryVo>> list() {
        log.info("查询商品类目信息");
        PageResult<CategoryVo> pageResult = categoryService.list();
        return Result.success(pageResult);
    }


    /**
     * @description: 查看类目等级为L1的id和名称信息
     * @param: request, response
     * @return: void
     */
    @GetMapping("/l1")
    public Result<PageResult<CategoryL1Vo>> l1() {
        log.info("查看类目等级为L1的信息");
        PageResult<CategoryL1Vo> pageResult = categoryService.L1Info();
        return Result.success(pageResult);
    }


    /**
     * @description: 新增商品类目信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/create")
    public Result<MarketCategory> create(@RequestBody MarketCategory category) {
        log.info("新增商品类目信息:{}", category);
        MarketCategory res = categoryService.addOne(category);
        return Result.success(res);
    }

    /**
     * @description:修改商品类目信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/update")
    public Result<MarketCategory> update(@RequestBody MarketCategory category) {
        log.info("修改商品类目信息:{}", category);
        boolean flag = categoryService.updateById(category);
        return flag ? Result.success(category) : Result.error("修改失败");
    }

    /**
     * @description: 根据id删除信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody MarketCategory category) throws IOException {
        log.info("删除商品类目信息:{}", category);
        boolean flag = categoryService.deleteById(category.getId());
        return flag ? Result.success() : Result.error("删除失败");
    }
}
